package com.credithc.signature.common.service;

import com.credithc.baseserv.core.utils.DateTimeUtils;
import com.credithc.signature.common.mapper.CertInfoMapper;
import com.credithc.signature.common.mapper.CertMapper;
import com.credithc.signature.common.po.Cert;
import com.credithc.signature.common.po.SealApplyInfoDTO;
import com.credithc.signature.common.po.SealInfo;
import com.credithc.signature.common.ro.sign.SealCertInfoRo;
import com.credithc.signature.common.to.CFCASignTO;
import com.credithc.signature.common.to.SealInfoDTO;
import com.credithc.signature.common.utils.DateUtil;
import com.credithc.signature.common.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
@Slf4j
public class CertInfoService {

    @Autowired
    private CertInfoMapper certInfoMapper;

    @Autowired
    private CertMapper certMapper;

    @Autowired
    SealInfoService sealInfoService;

    @Autowired
    CFCASignService cfcaSignService;

    /**
     * 获取证书信息
     */
    public SealApplyInfoDTO getSealInfo(String idTypeCode, String idNo, String userName) {
        return certInfoMapper.getSealInfo(idTypeCode, idNo, userName);
    }

    public SealApplyInfoDTO getSealInfo2(String idTypeCode, String idNo) {
        return certInfoMapper.getSealInfo2(idTypeCode, idNo);
    }

    /**
     * 获取未过期的证书信息
     */
    public SealApplyInfoDTO getUnExpireSealInfo(String idTypeCode, String idNo, String userName) {
        String expireTime = DateTimeUtils.getNowTime();
        return certInfoMapper.getUnExpireSealInfo(idTypeCode, idNo, userName, expireTime);
    }

    /**
     * 获取公章信息
     */
    public SealApplyInfoDTO getSealInfoByCode(String code) {
        return certInfoMapper.getSealInfoByCode(code);
    }

    public int updateByPrimaryKeySelective(SealApplyInfoDTO sealApplyInfoDTO) {
        return certInfoMapper.updateByPrimaryKeySelective(sealApplyInfoDTO);
    }

    public void updateCertDate(String requestSn, CFCASignTO cfcaSignTO) {
        try {
            SealCertInfoRo certInfoRo = cfcaSignService.querySealApplyInfo(requestSn, cfcaSignTO.getUserName(), cfcaSignTO.getIdTypeCode(), cfcaSignTO.getUserIdNo());
            if (certInfoRo != null) {
                SealApplyInfoDTO sealApplyInfoDTO = new SealApplyInfoDTO();
                String certNotBefore = certInfoRo.getCertNotBefore();
                String certNotAfter = certInfoRo.getCertNotAfter();
                certNotBefore = certNotBefore.substring(0, certNotBefore.indexOf("."));
                certNotAfter = certNotAfter.substring(0, certNotAfter.indexOf("."));
                Date notBefore = DateUtil.getDateFromStr(certNotBefore);
                Date notAfter = DateUtil.getDateFromStr(certNotAfter);
                String certSN = certInfoRo.getCertSN();
                String certDN = certInfoRo.getCertDN();
                sealApplyInfoDTO.setNotBefore(notBefore);
                sealApplyInfoDTO.setNotAfter(notAfter);
                sealApplyInfoDTO.setCertSn(certSN);
                sealApplyInfoDTO.setCertDn(certDN);
                sealApplyInfoDTO.setUpdateTime(new Date());
                sealApplyInfoDTO.setSealCode(cfcaSignTO.getSealCode());
                Example example = new Example(SealApplyInfoDTO.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("sealCode", cfcaSignTO.getSealCode());
                certInfoMapper.updateByExampleSelective(sealApplyInfoDTO, example);
            }
        } catch (Exception e) {
            log.error(LogUtil.outPut(requestSn, "更新私章信息失败,错误信息:" + e.getMessage()));
            SealApplyInfoDTO sealApplyInfoDTO = new SealApplyInfoDTO();
            String nowTime = DateTimeUtils.getNowTime();
            Date notBefore = DateUtil.getDateFromStr(nowTime);
            Date notAfter = DateUtil.getNextYear(DateUtil.getNextYear(notBefore));
            sealApplyInfoDTO.setNotBefore(notBefore);
            sealApplyInfoDTO.setNotAfter(notAfter);
            sealApplyInfoDTO.setUpdateTime(new Date());
            sealApplyInfoDTO.setSealCode(cfcaSignTO.getSealCode());
            Example example = new Example(SealApplyInfoDTO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("sealCode", cfcaSignTO.getSealCode());
            certInfoMapper.updateByExampleSelective(sealApplyInfoDTO, example);
        }
    }


    /**
     * 保存证书信息--返回主键
     *
     * @param cert
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int insertAndGetId(Cert cert) {
        return certMapper.insertAndGetId(cert);
    }

    /**
     * 根据sealCode删除
     *
     * @param sealCode
     */
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void deleteBySealCode(String sealCode) {
        try {
            SealInfo info = sealInfoService.selectBySealCode(sealCode);
            certMapper.deleteByPrimaryKey(info.getCertId());
            log.info("删除Cert表证书信息成功......");
        } catch (Exception e) {
            log.error("删除Cert表证书信息失败......");
        }
    }

    public void deleteByIdTypeAndNo(String idTypeCode, String userIdNo) {
        Example example = new Example(SealApplyInfoDTO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("identificationType", idTypeCode);
        criteria.andEqualTo("identificationNo", userIdNo);
        certInfoMapper.deleteByExample(example);
    }
}
